package com.sitechasia.webx.components.xtable.cell;

import org.extremecomponents.table.bean.Column;
import org.extremecomponents.table.cell.Cell;
import org.extremecomponents.table.core.TableModel;
import org.extremecomponents.table.view.html.ColumnBuilder;

public class SelectedCell implements Cell {
	public String getExportDisplay(TableModel model, Column column) {
		return null;
	}

	/**
	 * 在行中添加checkbox控件
	 */
	public String getHtmlDisplay(TableModel model, Column column) {

		ColumnBuilder columnBuilder = new ColumnBuilder(column);

		columnBuilder.tdStart();
		String id = column.getValueAsString();
		String controlName = column.getAlias() + "_selector";
		String selectableControlName = column.getAlias();


		columnBuilder.getHtmlBuilder().input("checkbox").name(
				selectableControlName).value(id);

		columnBuilder
				.getHtmlBuilder()
				.onclick(
						"if(this.checked){"
								+ "var  chkbxNum= document.getElementsByName('"
								+ selectableControlName
								+ "');"
								+ "var chckHeader= true;for (var i=0; i< chkbxNum.length; i++){chckHeader= chckHeader && chkbxNum[i].checked;}"
								+ "if(chckHeader)document.getElementById('"
								+ controlName
								+ "').checked = true;"
								+ "}else{document.getElementById('"
								+ controlName + "').checked=false;}");

		columnBuilder.getHtmlBuilder().xclose();

		columnBuilder.tdEnd();

		return columnBuilder.toString();
	}
}
